/*

 //                SimpleDateFormat formatdata = new SimpleDateFormat("dd/MM/yyyy");
 //                jTDataCompras.setText(formatdata.format(compras.getComDta()));
 */
package view;

import bean.Cliente;
import bean.Compras;
import bean.ComprasItem;
import bean.ComprasItemPK;
import bean.Produto;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Session;
import util.HibernateUtil;

/**
 *
 * @author Redação_RádioPérola
 */
public class viewCompras extends javax.swing.JFrame {

    /**
     * Creates new form viewCompras
     */
    public viewCompras() {
        //Look And Feell
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        initComponents();
        idGenerator();
        this.setLocationRelativeTo(this);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jProdutoCod = new javax.swing.JTextField();
        jDecriProduto = new javax.swing.JTextField();
        jValorU = new javax.swing.JTextField();
        jQtdeProduto = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jTotal = new javax.swing.JFormattedTextField();
        jTNumeroCompras = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTCodClienteCompras = new javax.swing.JTextField();
        jTNomeCompras = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTObservacaoCompras = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jBIniciarCompras = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTDataCompras = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableCompras = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Lançamento de Compras"));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel6.setText("Produto:");

        jProdutoCod.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jProdutoCodFocusLost(evt);
            }
        });

        jValorU.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jValorUFocusLost(evt);
            }
        });

        jLabel8.setText("Quantidade:");

        jLabel10.setText("Total R$:");

        jLabel7.setText("Descrição:");

        jLabel9.setText("Valor Unitário:");

        jTotal.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("¤#,##0.00"))));
        jTotal.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTotalFocusGained(evt);
            }
        });

        jTNumeroCompras.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTNumeroComprasFocusLost(evt);
            }
        });

        jLabel1.setText("Número");

        jLabel2.setText("Data atual");

        jLabel3.setText("Cliente");

        jTCodClienteCompras.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTCodClienteComprasFocusLost(evt);
            }
        });

        jLabel5.setText("Nome");

        jLabel4.setText("Observação");

        jBIniciarCompras.setText("Nova Compra");
        jBIniciarCompras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBIniciarComprasActionPerformed(evt);
            }
        });

        jButton2.setText("Salvar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(jLabel10)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jQtdeProduto, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jProdutoCod, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(57, 57, 57)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jDecriProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jValorU, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel4)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTNumeroCompras, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jTDataCompras, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jTCodClienteCompras, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(23, 23, 23)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jTNomeCompras, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jTObservacaoCompras, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jBIniciarCompras)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)))
                .addGap(0, 30, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTNumeroCompras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTCodClienteCompras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTNomeCompras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTDataCompras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTObservacaoCompras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jProdutoCod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jDecriProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jQtdeProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jValorU, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBIniciarCompras)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        jTabbedPane2.addTab("Compras", jPanel2);

        jTableCompras.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Produto", "Descrição", "Qtde", "Valor Unitário", "Total R$"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Double.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                true, true, true, true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableCompras.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(jTableCompras);
        jTableCompras.getColumnModel().getColumn(0).setResizable(false);
        jTableCompras.getColumnModel().getColumn(1).setResizable(false);
        jTableCompras.getColumnModel().getColumn(2).setResizable(false);
        jTableCompras.getColumnModel().getColumn(3).setResizable(false);
        jTableCompras.getColumnModel().getColumn(4).setResizable(false);

        jButton1.setText("Mostrar Compras");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 524, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(0, 1, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(0, 17, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab("Compras Lançadas", jPanel3);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 530, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 365, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 10, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTNumeroComprasFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTNumeroComprasFocusLost
    }//GEN-LAST:event_jTNumeroComprasFocusLost

    private void jTCodClienteComprasFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTCodClienteComprasFocusLost
        if (jTCodClienteCompras.getText().equals("") || jTCodClienteCompras.getText().trim() == null) {
        } else {

            Boolean verif = false;
            Session session = HibernateUtil.getSession();
            List<Cliente> listacli = session.createCriteria(Cliente.class).list();
            for (Cliente compras : listacli) {
                if (jTNumeroCompras.getText().trim().equals(Integer.toString(compras.getCliCod())) || (jTCodClienteCompras.getText().trim().equals(Integer.toString(compras.getCliCod())))) {
                    jTNomeCompras.setText(compras.getCliNom());
                    verif = true;
                }
            }

            if (verif == false) {
                JOptionPane.showMessageDialog(null, "Cliente Não Encontrado!");
                jTCodClienteCompras.requestFocus();
                jTCodClienteCompras.setText(null);
                jTNomeCompras.setText(null);
            }
            session.flush();
            session.close();
        }

    }//GEN-LAST:event_jTCodClienteComprasFocusLost

    private void jBIniciarComprasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBIniciarComprasActionPerformed
        limpaCampo();
    }//GEN-LAST:event_jBIniciarComprasActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Commit1();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        preencherTable();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jProdutoCodFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jProdutoCodFocusLost
        Boolean t = false;
        Session session = HibernateUtil.getSession();
        List<Produto> listaprod = session.createCriteria(Produto.class).list();
        for (Produto produtos : listaprod) {
            if (jProdutoCod.getText().trim().equals(Integer.toString(produtos.getProCod()))) {
                jDecriProduto.setText(produtos.getProDes());
                t = true;
            }
        }
        if (t == false) {
            JOptionPane.showMessageDialog(null, "Produto Nao Encontrado!");
            jDecriProduto.setText(null);
            jProdutoCod.requestFocus();
        }

        session.flush();

        session.close();
    }//GEN-LAST:event_jProdutoCodFocusLost

    private void jValorUFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jValorUFocusLost
        String valor = Double.toString(Double.parseDouble(jQtdeProduto.getText()) * Double.parseDouble(jValorU.getText()));

        jTotal.setText(valor);

    }//GEN-LAST:event_jValorUFocusLost

    private void jTotalFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTotalFocusGained
    }//GEN-LAST:event_jTotalFocusGained

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(viewCompras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(viewCompras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(viewCompras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(viewCompras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new viewCompras().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBIniciarCompras;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JTextField jDecriProduto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jProdutoCod;
    private javax.swing.JTextField jQtdeProduto;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTCodClienteCompras;
    private javax.swing.JTextField jTDataCompras;
    private javax.swing.JTextField jTNomeCompras;
    private javax.swing.JTextField jTNumeroCompras;
    private javax.swing.JTextField jTObservacaoCompras;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTable jTableCompras;
    private javax.swing.JFormattedTextField jTotal;
    private javax.swing.JTextField jValorU;
    // End of variables declaration//GEN-END:variables

    public void preencherTable() {
        Boolean Verif = false;
        DefaultTableModel modelo = (DefaultTableModel) jTableCompras.getModel();
        Session session = HibernateUtil.getSession();
        List<Produto> listaP = session.createCriteria(Produto.class).list();// Irá fazer uma Lista
        List<ComprasItem> lista = session.createCriteria(ComprasItem.class).list();// Iráma Lista fazer u
        modelo.setNumRows(0);
        for (ComprasItem comprasItem : lista) {
            for (Produto produto : listaP) {
                if (comprasItem.getComprasItemPK().getComPro() == produto.getProCod()) {
                    modelo.addRow(new Object[]{comprasItem.getComprasItemPK().getComCod(), produto.getProDes(), comprasItem.getComQtd(), comprasItem.getComVlr(), (comprasItem.getComQtd() * comprasItem.getComVlr())});
                    Verif = true;
                }

            }

        }
        if (Verif == false) {
            JOptionPane.showMessageDialog(null, "Nenhum Registro Encontrado!");
        }
    }

    public void limpaCampo() {
        idGenerator();

        jTDataCompras.setText(null);
        jTCodClienteCompras.setText(null);
        jTObservacaoCompras.setText(null);
        jTNomeCompras.setText(null);
        jProdutoCod.setText(null);
        jDecriProduto.setText(null);
        jQtdeProduto.setText(null);
        jValorU.setText(null);
        jTotal.setText(null);

    }

    public void Commit1() {
        Session session = HibernateUtil.getSession();
        Compras compras = new Compras();
        ComprasItem comprasitem = new ComprasItem();
        ComprasItemPK comprasitempk = new ComprasItemPK();

        compras.setComCod(Integer.parseInt(jTNumeroCompras.getText()));

        String data = jTDataCompras.getText().replaceAll("[/]", "");
        DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
        try {
            compras.setComDta(formatter.parse(jTDataCompras.getText()));
        } catch (ParseException ex) {
            Logger.getLogger(viewCompras.class.getName()).log(Level.SEVERE, null, ex);
        }

        compras.setComCli(Integer.parseInt(jTCodClienteCompras.getText()));

        compras.setComObs(jTObservacaoCompras.getText());
        //Gerador de ID do ComprasItens
        int i = 1;

        List<ComprasItem> lista = session.createCriteria(ComprasItem.class).list();// Irá fazer uma Lista
        for (ComprasItem Comprasitem : lista) {
            i++;
        }

        //FIM
        comprasitempk.setComCod(i);
        comprasitempk.setComPro(Integer.parseInt(jProdutoCod.getText()));
        comprasitem.setComQtd(Double.parseDouble(jQtdeProduto.getText()));
        comprasitem.setComVlr(Double.parseDouble(jValorU.getText()));
        comprasitem.setComprasItemPK(comprasitempk);

        //Commit no banco de dados.
        session.beginTransaction();
        session.saveOrUpdate(compras);
        session.saveOrUpdate(comprasitem);

        session.getTransaction().commit();
        JOptionPane.showMessageDialog(null, "Cadastrado com Sucesso!");
        session.flush();
        limpaCampo();
        session.close();
    }
    //Gerador Automatico de ID.

    private void idGenerator() {
        jTNumeroCompras.setEnabled(false);
        jDecriProduto.setEnabled(false);
        jTDataCompras.setEnabled(false);
        int i = 1;
        Session session = HibernateUtil.getSession();

        List<Compras> lista = session.createCriteria(Compras.class).list();// Irá fazer uma Lista
        for (Compras cadComp : lista) {
            i = cadComp.getComCod() + 1;

        }
        jTNumeroCompras.setText(Integer.toString(i));
        Date data = new Date(System.currentTimeMillis());
        SimpleDateFormat formatarDate = new SimpleDateFormat("MM/dd/yyyy");//Data - MM/dd/yy
        jTDataCompras.setText(formatarDate.format(data));
    }
}
